package com.adayo.memoryfree.activity;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.adayo.memoryfree.R;
import com.adayo.memoryfree.base.ActivityFrame;
import com.adayo.memoryfree.bean.AppProcessInfo;
import com.adayo.memoryfree.service.CoreService;
import com.adayo.memoryfree.utils.JToastKit;
import com.adayo.memoryfree.utils.JStorageKit;
import com.adayo.memoryfree.widgets.TitleView;

import org.xutils.view.annotation.ViewInject;

import java.util.List;

public class ShortCutActivity extends ActivityFrame implements CoreService.OnProcessActionListener {
	@ViewInject(R.id.tv_title)
	private TitleView titleView;
	@ViewInject(R.id.clean_light_img)
	ImageView cleanLightImg;
	private CoreService mCoreService;

	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mCoreService = ((CoreService.ProcessServiceBinder) service).getService();
			mCoreService.setOnActionListener(ShortCutActivity.this);
			mCoreService.cleanAllProcess();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mCoreService.setOnActionListener(null);
			mCoreService = null;
		}
	};

	@Override
	protected int getLayoutId() {
		return R.layout.activity_short_cut;
	}

	@Override
	protected void initParams(Bundle savedInstanceState) {
		titleView.setResource(R.drawable.selector_return_btn,
				R.string.short_cut_name);
		titleView.setLeftClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		cleanLightImg.startAnimation(AnimationUtils.loadAnimation(this,
				R.anim.rotate_anim));
		bindService(new Intent(context, CoreService.class),
				mServiceConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void onScanStarted(Context context) {

	}

	@Override
	public void onScanProgressUpdated(Context context, int current, int max) {

	}

	@Override
	public void onScanCompleted(Context context, List<AppProcessInfo> apps) {

	}

	@Override
	public void onCleanStarted(Context context) {

	}

	@Override
	public void onCleanCompleted(Context context, long cacheSize) {
		if (cacheSize > 0) {
			JToastKit.getInstance().showMessage("一键清理,为您释放" + JStorageKit.convertStorage(cacheSize) + "内存");
		} else {
			JToastKit.getInstance().showMessage("您刚刚清理过内存,请稍后再来~");
		}
		//finish();
	}

	private void killProcess() {
		ActivityManager am = (ActivityManager) getBaseContext()
				.getApplicationContext().getSystemService(
						Context.ACTIVITY_SERVICE);
		// 获得正在运行的所有进程
		List<ActivityManager.RunningAppProcessInfo> processes = am
				.getRunningAppProcesses();

		for (ActivityManager.RunningAppProcessInfo info : processes) {
			if (info != null && info.processName != null
					&& info.processName.length() > 0) {
				String pkgName = info.processName;
				if (!("system".equals(pkgName) || "launcher".equals(pkgName)
						|| "android.process.media".equals(pkgName)
						|| "android.process.acore".equals(pkgName)
						|| "com.android.phone".equals(pkgName)
						|| "com.fb.FileBrower".equals(pkgName)// 浏览器
						|| "com.ott_pro.launcher".equals(pkgName)// 桌面
						|| "com.ott_pro.upgrade".equals(pkgName)// 升级
						|| "com.example.airplay".equals(pkgName)// 媒体分享
						|| "com.amlogic.mediacenter".equals(pkgName)// 媒体分享
						|| "com.android.dreams.phototable".equals(pkgName)// 屏保
						|| "com.amlogic.inputmethod.remote".equals(pkgName)// 输入法
						|| pkgName.startsWith("com.lefter"))) {
					am.killBackgroundProcesses(pkgName);// 杀进程
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		unbindService(mServiceConnection);
		super.onDestroy();
	}
}
